/*
 * @Author: your name
 * @Date: 2021-09-12 11:33:11
 * @LastEditTime: 2022-05-17 14:51:58
 * @LastEditors: 夏郑兴 971348024@qq.com
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: /gitee-project/whale-admin-web/src/store/modules/user.ts
 */
import { loginApi, getInfoApi, loginOutApi } from '@/api/user'
import { ActionContext } from 'vuex'
import { whaleYunLogin } from '@/api/service/login'

interface State {
  token: string,
  info: object,
  editAdminInfo: object,
  adminInfo: object,
  projectDetailInfo: object,
  topicInfo: object,
}
const state = (): State => ({
  token: '', // 登录token
  info: {}, // 用户信息
  editAdminInfo: {}, // 需要编辑管理信息
  adminInfo: {}, // 登录获取的用户信息及权限
  projectDetailInfo: {}, // 项目详情数据
  topicInfo: {} // 题目详情数据
})

// getters
const getters = {
  token(state: State) {
    return state.token
  }
}

// mutations
const mutations = {
  tokenChange(state: State, token: string) {
    state.token = token
  },
  infoChange(state: State, info: object) {
    state.info = info
  },
  editAdminInfoChange(state: State, editAdminInfo: object) {
    state.editAdminInfo = editAdminInfo
  },
  adminInfoChange(state: State, adminInfo: object) {
    state.adminInfo = adminInfo
  },
  projectDetailInfoChange(state: State, projectDetailInfo: object) {
    state.projectDetailInfo = projectDetailInfo
  },
  topicInfoChange(state: State, topicInfo: object) {
    state.topicInfo = topicInfo
  },
}

// actions
const actions = {
  whaleYunLoginAction({ commit, dispatch }: ActionContext<State, State>, data: any) {
    return new Promise((resolve, reject) => {
      whaleYunLogin(data)
        .then(res => {
          commit('tokenChange', res.data)
          resolve(res.data)
        }).catch(err => {
          reject(err)
        })
    })
  },
  // login by login.vue
  login({ commit, dispatch }: ActionContext<State, State>, params: any) {
    return new Promise((resolve, reject) => {
      loginApi(params)
        .then(res => {
          commit('tokenChange', res.data.token)
          dispatch('getInfo', { token: res.data.token })
            .then(infoRes => {
              resolve(res.data.token)
            })
        }).catch(err => {
          reject(err)
        })
    })
  },
  // get user info after user logined
  getInfo({ commit }: ActionContext<State, State>, params: any) {
    return new Promise((resolve, reject) => {
      getInfoApi(params)
        .then(res => {
          commit('infoChange', res.data.info)
          resolve(res.data.info)
        })
    })
  },

  // login out the system after user click the loginOut button
  loginOut({ commit }: ActionContext<State, State>) {
    loginOutApi()
      .then(res => {

      })
      .catch(error => {

      })
      .finally(() => {
        localStorage.removeItem('tabs')
        localStorage.removeItem('vuex')
        localStorage.removeItem('Auth')
        localStorage.removeItem('LoginInfo')
        sessionStorage.removeItem('vuex')
        location.reload()
      })
  }
}

export default {
  namespaced: true,
  state,
  actions,
  getters,
  mutations
}
